﻿
using ControlzEx.Standard;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Threading;
using ZDK.Infra.Constants;
using ZDK.Infra.Events;
using ZDK.Infra.Helpers;
using ZDK.Infra.Models;
using ZDK.Infra.Services;
using ZDK.Models.Packages;
using ZDK.Utilities;
using static log4net.Appender.RollingFileAppender;
using static ZDK.Models.Packages.BasePkt;

namespace ZDK.ViewModels
{
    public class FuncViewModel : BindableBase
    {
        readonly IMyLog _log;
        readonly IEventAggregator _evtAggr;
        readonly ICfgService _cfgSvr;

        private string _selClientKey;
        private string _TerminalId;
        private bool canCommu = false;
        public bool CanCommu { get => canCommu; set => SetProperty(ref canCommu, value); }

        readonly Dispatcher _dispatcher = Dispatcher.CurrentDispatcher;

        public Dictionary<string, string> _keyTermIdDict { get; set; }
        private BasePkt _curPkt; //当前处理的功能，针对有按钮的afn

        #region 属性绑定

        #region Afn01

        private Afn01_F1ResetEnum afn01F1ResetMode = Afn01_F1ResetEnum.REBOOT;
        public Afn01_F1ResetEnum Afn01F1ResetMode { get => afn01F1ResetMode; set => SetProperty(ref afn01F1ResetMode, value); }

        private Afn01_F3ResetModularEnum afn01F3ResetModularMode = Afn01_F3ResetModularEnum.TERMINAL_CONTROL;
        public Afn01_F3ResetModularEnum Afn01F3ResetModularMode { get => afn01F3ResetModularMode; set => SetProperty(ref afn01F3ResetModularMode, value); }

        private string afn01F1Response;
        public string Afn01F1Response { get => afn01F1Response; set => SetProperty(ref afn01F1Response, value); }

        private string afn01F2Response;
        public string Afn01F2Response { get => afn01F2Response; set => SetProperty(ref afn01F2Response, value); }
        private string afn01F3Response;
        public string Afn01F3Response { get => afn01F3Response; set => SetProperty(ref afn01F3Response, value); }

        #endregion

        #region Afn02
        private string afn02F1LoginTime;
        public string Afn02F1LoginTime { get => afn02F1LoginTime; set => SetProperty(ref afn02F1LoginTime, value); }

        private string afn02F2LogoutTime;
        public string Afn02F2LogoutTime { get => afn02F2LogoutTime; set => SetProperty(ref afn02F2LogoutTime, value); }

        private string afn02F3HeartbeatTime;
        public string Afn02F3HeartbeatTime { get => afn02F3HeartbeatTime; set => SetProperty(ref afn02F3HeartbeatTime, value); }

        #endregion

        #region Afn04/0A

        #region Afn04F1
        private string Afn04F1timeout1 = "10";

        public string Afn04F1timeout { get => Afn04F1timeout1; set => SetProperty(ref Afn04F1timeout1, value); }

        private string Afn04F1repeatlimit1 = "3";

        public string Afn04F1repeatlimit { get => Afn04F1repeatlimit1; set => SetProperty(ref Afn04F1repeatlimit1, value); }

        private string Afn04F1con1 = "4";

        public string Afn04F1con { get => Afn04F1con1; set => SetProperty(ref Afn04F1con1, value); }

        private string Afn04F1heartinterval1 = "2";

        public string Afn04F1heartinterval { get => Afn04F1heartinterval1; set => SetProperty(ref Afn04F1heartinterval1, value); }

        private string primaryIp1 = "10.140.1.132";

        public string Afn04F2primaryIp { get => primaryIp1; set => SetProperty(ref primaryIp1, value); }
        #endregion

        #region Afn04F2
        private string primaryPort1 = "5009";

        public string Afn04F2primaryPort { get => primaryPort1; set => SetProperty(ref primaryPort1, value); }

        private string aFN = "shcs02s.shiot";

        public string Afn04F2AFN { get => aFN; set => SetProperty(ref aFN, value); }

        private string username1 = "noUserName";

        public string Afn04F2username { get => username1; set => SetProperty(ref username1, value); }

        private string pwd = "noPassWord";

        public string Afn04F2Pwd { get => pwd; set => SetProperty(ref pwd, value); }

        private string realmname1 = "noDomainName1";

        public string Afn04F2realmname { get => realmname1; set => SetProperty(ref realmname1, value); }

        private string secondaryIp1 = "180.167.245.226";

        public string Afn04F2secondaryIp { get => secondaryIp1; set => SetProperty(ref secondaryIp1, value); }

        private string Afn04F2realmname21 = "noDomainName2";

        public string Afn04F2realmname2 { get => Afn04F2realmname21; set => SetProperty(ref Afn04F2realmname21, value); }

        private string Afn04F2secondaryPort1 = "15010";

        public string Afn04F2secondaryPort { get => Afn04F2secondaryPort1; set => SetProperty(ref Afn04F2secondaryPort1, value); }
        #endregion

        #region Afn04F3
        private string Afn04F3important1 = "1149543804886319105";

        public string Afn04F3important { get => Afn04F3important1; set => SetProperty(ref Afn04F3important1, value); }

        private string Afn04F3initiative1 = "575334852396580865";

        public string Afn04F3initiative { get => Afn04F3initiative1; set => SetProperty(ref Afn04F3initiative1, value); }

        private string Afn04F3effect1 = "573083052582895617";

        public string Afn04F3effect { get => Afn04F3effect1; set => SetProperty(ref Afn04F3effect1, value); }
        #endregion

        #region Afn04F73
        private string Afn04F73inlines1 = "2";

        public string Afn04F73inlines { get => Afn04F73inlines1; set => SetProperty(ref Afn04F73inlines1, value); }

        private string Afn04F73temppoints1 = "3";

        public string Afn04F73temppoints { get => Afn04F73temppoints1; set => SetProperty(ref Afn04F73temppoints1, value); }

        private string Afn04F73usercages1 = "4";

        public string Afn04F73usercages { get => Afn04F73usercages1; set => SetProperty(ref Afn04F73usercages1, value); }

        private string Afn04F73eleclock1 = "2";

        public string Afn04F73eleclock { get => Afn04F73eleclock1; set => SetProperty(ref Afn04F73eleclock1, value); }
        #endregion

        #region Afn04F74
        private string Afn04F74templim1 = "55";

        public string Afn04F74templim { get => Afn04F74templim1; set => SetProperty(ref Afn04F74templim1, value); }

        private string Afn04F74temprecover1 = "50";

        public string Afn04F74temprecover { get => Afn04F74temprecover1; set => SetProperty(ref Afn04F74temprecover1, value); }

        private string Afn04F74wetlim1 = "75";

        public string Afn04F74wetlim { get => Afn04F74wetlim1; set => SetProperty(ref Afn04F74wetlim1, value); }

        private string Afn04F74wetrecover1 = "74";

        public string Afn04F74wetrecover { get => Afn04F74wetrecover1; set => SetProperty(ref Afn04F74wetrecover1, value); }

        private string Afn04F74AC220limon1 = "236.0";

        public string Afn04F74AC220limon { get => Afn04F74AC220limon1; set => SetProperty(ref Afn04F74AC220limon1, value); }

        private string Afn04F74AC220limoff1 = "198.0";

        public string Afn04F74AC220limoff { get => Afn04F74AC220limoff1; set => SetProperty(ref Afn04F74AC220limoff1, value); }

        private string Afn04F74AC220recoveron1 = "229.0";

        public string Afn04F74AC220recoveron { get => Afn04F74AC220recoveron1; set => SetProperty(ref Afn04F74AC220recoveron1, value); }

        private string Afn04F74AC220recoveroff1 = "204.0";

        public string Afn04F74AC220recoveroff { get => Afn04F74AC220recoveroff1; set => SetProperty(ref Afn04F74AC220recoveroff1, value); }

        private string Afn04F74AC24lim1 = "25.6";

        public string Afn04F74AC24limon { get => Afn04F74AC24lim1; set => SetProperty(ref Afn04F74AC24lim1, value); }

        private string Afn04F74AC24recover1 = "24.9";

        public string Afn04F74AC24recoveron { get => Afn04F74AC24recover1; set => SetProperty(ref Afn04F74AC24recover1, value); }

        private string Afn04F74AC24limoff1 = "22.3";

        public string Afn04F74AC24limoff { get => Afn04F74AC24limoff1; set => SetProperty(ref Afn04F74AC24limoff1, value); }

        private string Afn04F74AC24recoveroff1 = "23.0";

        public string Afn04F74AC24recoveroff { get => Afn04F74AC24recoveroff1; set => SetProperty(ref Afn04F74AC24recoveroff1, value); }

        private string Afn04F74DC24limon1 = "12.8";

        public string Afn04F74DC24limon { get => Afn04F74DC24limon1; set => SetProperty(ref Afn04F74DC24limon1, value); }

        private string Afn04F74DC24recoveron1 = "12.5";

        public string Afn04F74DC24recoveron { get => Afn04F74DC24recoveron1; set => SetProperty(ref Afn04F74DC24recoveron1, value); }

        private string Afn04F74DC24recoveroff1 = "11.5";

        public string Afn04F74DC24recoveroff { get => Afn04F74DC24recoveroff1; set => SetProperty(ref Afn04F74DC24recoveroff1, value); }

        private string Afn04F74DC24limoff1 = "11.2";

        public string Afn04F74DC24limoff { get => Afn04F74DC24limoff1; set => SetProperty(ref Afn04F74DC24limoff1, value); }

        private string Afn04F74usercagelim1 = "500";

        public string Afn04F74usercagelim { get => Afn04F74usercagelim1; set => SetProperty(ref Afn04F74usercagelim1, value); }

        private string Afn04F74usercagerecover1 = "485";

        public string Afn04F74usercagerecover { get => Afn04F74usercagerecover1; set => SetProperty(ref Afn04F74usercagerecover1, value); }

        private string Afn04F74overloadtime1 = "2";

        public string Afn04F74overloadtime { get => Afn04F74overloadtime1; set => SetProperty(ref Afn04F74overloadtime1, value); }

        private string Afn04F74fanerror1 = "75";

        public string Afn04F74fanerror { get => Afn04F74fanerror1; set => SetProperty(ref Afn04F74fanerror1, value); }
        #endregion

        #region Afn04F75
        private string Afn04F75fanopentemp1 = "40";

        public string Afn04F75fanopentemp { get => Afn04F75fanopentemp1; set => SetProperty(ref Afn04F75fanopentemp1, value); }

        private string Afn04F75fanclosetemp1 = "35";

        public string Afn04F75fanclosetemp { get => Afn04F75fanclosetemp1; set => SetProperty(ref Afn04F75fanclosetemp1, value); }

        private string Afn04F75lasttime1 = "10";

        public string Afn04F75lasttime { get => Afn04F75lasttime1; set => SetProperty(ref Afn04F75lasttime1, value); }
        #endregion

        #region Afn04F76
        private Afn04_F76DoorEnum door76 = Afn04_F76DoorEnum.Door1;
        public Afn04_F76DoorEnum Afn04F76Door { get => door76; set => SetProperty(ref door76, value); }

        private string Afn04F76eleclockdevice1 = "00002466";

        public string Afn04F76eleclockdevice { get => Afn04F76eleclockdevice1; set => SetProperty(ref Afn04F76eleclockdevice1, value); }

        private string Afn04F76atuoclose1 = "10";

        public string Afn04F76atuoclose { get => Afn04F76atuoclose1; set => SetProperty(ref Afn04F76atuoclose1, value); }

        private string Afn04F76alarmtime1 = "30";

        public string Afn04F76alarmtime { get => Afn04F76alarmtime1; set => SetProperty(ref Afn04F76alarmtime1, value); }

        private string Afn04F76eleclockID1 = "00002466";

        public string Afn04F76eleclockID { get => Afn04F76eleclockID1; set => SetProperty(ref Afn04F76eleclockID1, value); } 
        #endregion
        
        #region Afn04F80
        private string _Afn04F80manuId = "CETC";
        public string Afn04F80manuId { get => _Afn04F80manuId; set => SetProperty(ref _Afn04F80manuId, value); }
        private string _Afn04F80deviceId = "00000001";
        public string Afn04F80deviceId { get => _Afn04F80deviceId; set => SetProperty(ref _Afn04F80deviceId, value); }

        private string _Afn04F80version = "0000V1.2";
        public string Afn04F80version { get => _Afn04F80version; set => SetProperty(ref _Afn04F80version, value); }
        private string _Afn04F80publishDate = "22-10-21";
        public string Afn04F80publishDate { get => _Afn04F80publishDate; set => SetProperty(ref _Afn04F80publishDate, value); }
        private string _Afn04F80protocolVer = "DLZM0000";
        public string Afn04F80protocolVer { get => _Afn04F80protocolVer; set => SetProperty(ref _Afn04F80protocolVer, value); }
        private string _Afn04F80hardwareVer = "V2.00000";
        public string Afn04F80hardwareVer { get => _Afn04F80hardwareVer; set => SetProperty(ref _Afn04F80hardwareVer, value); }
        private string _Afn04F80hardwarePublish = "22-10-21";
        public string Afn04F80hardwarePublish { get => _Afn04F80hardwarePublish; set => SetProperty(ref _Afn04F80hardwarePublish, value); }
        private string _Afn04F80leaveFactory = "19-10-21";
        public string Afn04F80leaveFactory { get => _Afn04F80leaveFactory; set => SetProperty(ref _Afn04F80leaveFactory, value); }

        private string _Afn04F80ip = "192.168.0.126";
        public string Afn04F80ip { get => _Afn04F80ip; set => SetProperty(ref _Afn04F80ip, value); }

        private string _Afn04F80gateway = "192.168.0.1";
        public string Afn04F80gateway { get => _Afn04F80gateway; set => SetProperty(ref _Afn04F80gateway, value); }

        private string _Afn04F80submask = "255.255.255.0";
        public string Afn04F80submask { get => _Afn04F80submask; set => SetProperty(ref _Afn04F80submask, value); }
        private string _Afn04F80mac = "58-A0-23-5C-9E-AF";
        public string Afn04F80mac { get => _Afn04F80mac; set => SetProperty(ref _Afn04F80mac, value); }
        private List<string> Afn04F80BaudList1 = new List<string>() { "4800", "9600", "38400", "115200" };
        public List<string> Afn04F80BaudList { get => Afn04F80BaudList1; set => SetProperty(ref Afn04F80BaudList1, value); }
        private string _Afn04F80SelbaudRate1 = "9600";
        public string Afn04F80SelbaudRate1 { get => _Afn04F80SelbaudRate1; set => SetProperty(ref _Afn04F80SelbaudRate1, value); }
        private string _Afn04F80SelbaudRate2 = "9600";
        public string Afn04F80SelbaudRate2 { get => _Afn04F80SelbaudRate2; set => SetProperty(ref _Afn04F80SelbaudRate2, value); }
        private string _Afn04F80SelbaudRate3 = "9600";
        public string Afn04F80SelbaudRate3 { get => _Afn04F80SelbaudRate3; set => SetProperty(ref _Afn04F80SelbaudRate3, value); }
        private int _Afn04F80svrBReconnTime = 5;
        public int Afn04F80svrBReconnTime { get => _Afn04F80svrBReconnTime; set => SetProperty(ref _Afn04F80svrBReconnTime, value); }
        private int _Afn04F80svrAReconnTime = 5;
        public int Afn04F80svrAReconnTime { get => _Afn04F80svrAReconnTime; set => SetProperty(ref _Afn04F80svrAReconnTime, value); }
        private int _Afn04F80svrBConnTimeout = 5;
        public int Afn04F80svrBConnTimeout { get => _Afn04F80svrBConnTimeout; set => SetProperty(ref _Afn04F80svrBConnTimeout, value); }
        private int _Afn04F80svrAConnTimeout = 5;
        public int Afn04F80svrAConnTimeout { get => _Afn04F80svrAConnTimeout; set => SetProperty(ref _Afn04F80svrAConnTimeout, value); }
        private int _Afn04F80maxReconnCount = 60;
        public int Afn04F80maxReconnCount { get => _Afn04F80maxReconnCount; set => SetProperty(ref _Afn04F80maxReconnCount, value); }
        private string _Afn04F80terminalId = "00000120003488 ";
        public string Afn04F80terminalId { get => _Afn04F80terminalId; set => SetProperty(ref _Afn04F80terminalId, value); } 
        #endregion
        
        private string afn04F1Response;
        public string Afn04F1Response { get => afn04F1Response; set => SetProperty(ref afn04F1Response, value); }

        private string afn04F2Response;
        public string Afn04F2Response { get => afn04F2Response; set => SetProperty(ref afn04F2Response, value); }

        private string afn04F3Response;
        public string Afn04F3Response { get => afn04F3Response; set => SetProperty(ref afn04F3Response, value); }

        private string afn04F73Response;
        public string Afn04F73Response { get => afn04F73Response; set => SetProperty(ref afn04F73Response, value); }

        private string afn04F74Response;
        public string Afn04F74Response { get => afn04F74Response; set => SetProperty(ref afn04F74Response, value); }

        private string afn04F75Response;
        public string Afn04F75Response { get => afn04F75Response; set => SetProperty(ref afn04F75Response, value); }

        private string afn04F76Response;
        public string Afn04F76Response { get => afn04F76Response; set => SetProperty(ref afn04F76Response, value); }

        private string afn04F80Response;
        public string Afn04F80Response { get => afn04F80Response; set => SetProperty(ref afn04F80Response, value); }
        #endregion

        #region Afn05
        private string afn05F31Response;
        public string Afn05F31Response { get => afn05F31Response; set => SetProperty(ref afn05F31Response, value); }
        private string afn05F73Response;
        public string Afn05F73Response { get => afn05F73Response; set => SetProperty(ref afn05F73Response, value); }
        private Afn05_F74InlineNoEnum afn05F74InlineNo = Afn05_F74InlineNoEnum.Inline1;
        public Afn05_F74InlineNoEnum Afn05F74InlineNo { get => afn05F74InlineNo; set => SetProperty(ref afn05F74InlineNo, value); }
        private Afn05_F74OnOffEnum afn05F74InlineMode = Afn05_F74OnOffEnum.Close;
        public Afn05_F74OnOffEnum Afn05F74InlineMode { get => afn05F74InlineMode; set => SetProperty(ref afn05F74InlineMode, value); }
        private string afn05F74Response;
        public string Afn05F74Response { get => afn05F74Response; set => SetProperty(ref afn05F74Response, value); }
        private Afn05_F75CabinNoEnum afn05F75CabinNo = Afn05_F75CabinNoEnum.Cabin1;
        public Afn05_F75CabinNoEnum Afn05F75CabinNo { get => afn05F75CabinNo; set => SetProperty(ref afn05F75CabinNo, value); }
        private Afn05_F74OnOffEnum afn05F75OptType = Afn05_F74OnOffEnum.Close;
        public Afn05_F74OnOffEnum Afn05F75OptType { get => afn05F75OptType; set => SetProperty(ref afn05F75OptType, value); }
        private Afn05_F75BranchEnum afn05F75Branch = Afn05_F75BranchEnum.AC220;
        public Afn05_F75BranchEnum Afn05F75Branch { get => afn05F75Branch; set => SetProperty(ref afn05F75Branch, value); }
        private string afn05F75Response;
        public string Afn05F75Response { get => afn05F75Response; set => SetProperty(ref afn05F75Response, value); }

        #endregion

        #region Afn09
        private string afn09F1Response;
        public string Afn09F1Response { get => afn09F1Response; set => SetProperty(ref afn09F1Response, value); }

        private string afn09F9Response;
        public string Afn09F9Response { get => afn09F9Response; set => SetProperty(ref afn09F9Response, value); }
        #endregion

        #region Afn0C
        private string afn0CF2Response;
        public string Afn0CF2Response { get => afn0CF2Response; set => SetProperty(ref afn0CF2Response, value); }
        private Afn0C_F73BoxEnum afn0CF73Box = Afn0C_F73BoxEnum.Comm;
        public Afn0C_F73BoxEnum Afn0CF73Box { get => afn0CF73Box; set => SetProperty(ref afn0CF73Box, value); }

        private string afn0CF73Response;
        public string Afn0CF73Response { get => afn0CF73Response; set => SetProperty(ref afn0CF73Response, value); }
        private string afn0CF74Response;
        public string Afn0CF74Response { get => afn0CF74Response; set => SetProperty(ref afn0CF74Response, value); }
        private string afn0CF75Response;
        public string Afn0CF75Response { get => afn0CF75Response; set => SetProperty(ref afn0CF75Response, value); }
        private string afn0CF76Response;
        public string Afn0CF76Response { get => afn0CF76Response; set => SetProperty(ref afn0CF76Response, value); }
        private string afn0CF77Response;
        public string Afn0CF77Response { get => afn0CF77Response; set => SetProperty(ref afn0CF77Response, value); }
        private Afn04_F76DoorEnum afn0CF78Door = Afn04_F76DoorEnum.Door1;
        public Afn04_F76DoorEnum Afn0CF78Door { get => afn0CF78Door; set => SetProperty(ref afn0CF78Door, value); }

        private string afn0CF78Response;
        public string Afn0CF78Response { get => afn0CF78Response; set => SetProperty(ref afn0CF78Response, value); }
        private Afn05_F75CabinNoEnum afn0CF81CabinNo = Afn05_F75CabinNoEnum.Cabin1;
        public Afn05_F75CabinNoEnum Afn0CF81CabinNo { get => afn0CF81CabinNo; set => SetProperty(ref afn0CF81CabinNo, value); }
        private string afn0CF81Response;
        public string Afn0CF81Response { get => afn0CF81Response; set => SetProperty(ref afn0CF81Response, value); }
        private string afn0CF82Response;
        public string Afn0CF82Response { get => afn0CF82Response; set => SetProperty(ref afn0CF82Response, value); }
        private string afn0CF83Response;
        public string Afn0CF83Response { get => afn0CF83Response; set => SetProperty(ref afn0CF83Response, value); }
        private string afn0CF84Response;
        public string Afn0CF84Response { get => afn0CF84Response; set => SetProperty(ref afn0CF84Response, value); }
        private Afn05_F74InlineNoEnum afn0CF85InlineNo = Afn05_F74InlineNoEnum.Inline1;
        public Afn05_F74InlineNoEnum Afn0CF85InlineNo { get => afn0CF85InlineNo; set => SetProperty(ref afn0CF85InlineNo, value); }
        private string afn0CF85Response;
        public string Afn0CF85Response { get => afn0CF85Response; set => SetProperty(ref afn0CF85Response, value); }
        private string afn0CF86Response;
        public string Afn0CF86Response { get => afn0CF86Response; set => SetProperty(ref afn0CF86Response, value); }
        private string afn0CF87Response;
        public string Afn0CF87Response { get => afn0CF87Response; set => SetProperty(ref afn0CF87Response, value); }
        private string afn0CF88Response;
        public string Afn0CF88Response { get => afn0CF88Response; set => SetProperty(ref afn0CF88Response, value); }
        private string afn0CF89Response;
        public string Afn0CF89Response { get => afn0CF89Response; set => SetProperty(ref afn0CF89Response, value); }
        #endregion

        #endregion

        #region 构造
        public FuncViewModel(IMyLog log, IEventAggregator evtAggr, ICfgService cfgSvr)
        {
            _log = log;
            _evtAggr = evtAggr;
            _cfgSvr = cfgSvr;

            _evtAggr.GetEvent<Is4GModeEvent>().Subscribe(OnChange4GMode);
            _evtAggr.GetEvent<ClientKeySelChangedEvent>().Subscribe(ClientKeySelChanged);
            _evtAggr.GetEvent<OnConnEvent>().Subscribe(OnConn);
            _evtAggr.GetEvent<OnDisConnEvent>().Subscribe(OnDisConn);
            _evtAggr.GetEvent<RecvMsgEvent>().Subscribe(OnRecvMsg);

            _keyTermIdDict = new Dictionary<string, string>();
        } 
        #endregion

        #region Event
        void OnChange4GMode(bool mode)
        {
            //非4G模式，由用户指定终端id
            //4G模式，取afn02上传的终端id
            if (!mode)
            {
                var cfg = _cfgSvr.Get();
                _TerminalId = cfg.net.terminalId;
                CanCommu = true;
            } 
        }
        void ClientKeySelChanged(string clientKey)
        {
            _selClientKey = clientKey;
        }
        void OnConn(string clientKey)
        {
            _keyTermIdDict.Add(clientKey, "");
        }
        void OnDisConn(string clientKey)
        {
            if (_keyTermIdDict.ContainsKey(clientKey))
                _keyTermIdDict.Remove(clientKey);
        }
        void OnRecvMsg(RecvMsgEventPayLoad payload)
        {
            var dt = payload.data;
            string clientKey = payload.key;
            string s = "";

            //校验报文
            bool bOk = PktUtils.ValidPkg(dt, ref s);
            if (!bOk)
            {
                _dispatcher.Invoke(new Action(() =>
                {
                    _log.Error(s);
                }));
                return;
            }

            BasePkt pkt = null; //输出用，内部赋值
            try
            {
                s = PktUtils.TranslatePkg(dt, ref pkt);
                if (pkt == null)
                {
                    _dispatcher.Invoke(new Action(() =>
                    {
                        _log.Error(s);
                    }));
                    return;
                }
                //输出通用部分
                _dispatcher.Invoke(new Action(() =>
                {
                    _log.Info($"报文解析→终端id：{pkt.CtrlField.TerminalID}，终端类型：{pkt.CtrlField.TerminalType}H，{(pkt.CtrlField.Dir == 0 ? "下行报文" : "上行报文")}，来自{(pkt.CtrlField.PRM == 0 ? "从动站" : "启动站")}，{(pkt.CtrlField.ACD == 0 ? "无事件" : "有事件")}，{(pkt.CtrlField.EncryptionType == 0 ? "不加密" : "加密")}，版本{pkt.CtrlField.Version}，帧流水号：{pkt.CtrlField.FrameSerialNum}，功能码：{pkt.Afn:X2}H，{(pkt.SEQ_TpV == 0 ? "无Tp" : "有Tp")}，{pkt.SEQ_Flag}，{(pkt.SEQ_CON == 0 ? "无需" : "需")}确认回复，Fn：{pkt.Fn}，Pn：{pkt.Pn}，时间：{pkt.TimeStr}");
                }));

                switch (pkt.Afn)
                {
                    case Comm.Afn02:
                        {
                            if (_keyTermIdDict.ContainsKey(clientKey))
                            {
                                _keyTermIdDict[clientKey] = pkt.CtrlField.TerminalID;
                            }
                            else
                            {
                                _keyTermIdDict.Add(clientKey, pkt.CtrlField.TerminalID);
                            }
                            switch (pkt.Fn)
                            {
                                case 1: Afn02F1LoginTime = s; break;
                                case 2: Afn02F2LogoutTime = s; break;
                                case 3: Afn02F3HeartbeatTime = s; break;
                            }
                            //回复00H（复用pkt）
                            pkt.Afn = Comm.Afn00; pkt.Pn = 0; pkt.Fn = 1;
                            byte[] data = PktUtils.MakeSendPkg(pkt);
                            SendData(data);
                            CanCommu = clientKey == null ? false : true;
                            break;
                        }
                }
                if (_curPkt == null) return;
                //输出业务部分
                switch (_curPkt.Afn)
                {
                    case Comm.Afn01:
                        {
                            switch (_curPkt.Fn)
                            {
                                case 1: Afn01F1Response = s; break;
                                case 2: Afn01F2Response = s; break;
                                case 3: Afn01F3Response = s; break;
                            }
                            break;
                        }
                    case Comm.Afn04:
                        {
                            switch (_curPkt.Fn)
                            {
                                case 1: Afn04F1Response = s; break;
                                case 2: Afn04F2Response = s; break;
                                case 3: Afn04F3Response = s; break;
                                case 73: Afn04F73Response = s; break;
                                case 74: Afn04F74Response = s; break;
                                case 75: Afn04F75Response = s; break;
                                case 76: Afn04F76Response = s; break;
                                case 80: Afn04F80Response = s; break;
                            }
                            break;
                        }
                    case Comm.Afn05:
                        {
                            switch (_curPkt.Fn)
                            {
                                case 31: Afn05F31Response = s; break;
                                case 73: Afn05F73Response = s; break;
                                case 74: Afn05F74Response = s; break;
                                case 75: Afn05F75Response = s; break;
                            }
                            break;
                        }
                    case Comm.Afn09:
                        {
                            ST_Afn09 st = (ST_Afn09)pkt;
                            string updateTIme = $"更新时间：{st.TimeStr}";
                            switch (_curPkt.Fn)
                            {
                                case 1:
                                    {
                                        Afn09F1Response = s; break;
                                    }
                                case 9:
                                    {
                                        Afn09F9Response = s; break;
                                    }
                                case 80:
                                    {
                                        Afn04F80manuId = st.F80_manuId;
                                        Afn04F80deviceId = st.F80_deviceId;
                                        Afn04F80version = st.F80_version;
                                        Afn04F80publishDate = st.F80_publishDate;
                                        Afn04F80protocolVer = st.F80_protocolVer;
                                        Afn04F80hardwareVer = st.F80_hardwareVer;
                                        Afn04F80hardwarePublish = st.F80_hardwarePublish;
                                        Afn04F80leaveFactory = st.F80_leaveFactory;
                                        Afn04F80ip = st.F80_ip;
                                        Afn04F80gateway = st.F80_gateway;
                                        Afn04F80submask = st.F80_submask;
                                        Afn04F80mac = st.F80_mac;
                                        Afn04F80SelbaudRate1 = PktUtils.getBaudRateStr(st.F80_baudRate1);
                                        Afn04F80SelbaudRate2 = PktUtils.getBaudRateStr(st.F80_baudRate2);
                                        Afn04F80SelbaudRate3 = PktUtils.getBaudRateStr(st.F80_baudRate3);
                                        Afn04F80svrAReconnTime = st.F80_svrAConnTimeout;
                                        Afn04F80svrBReconnTime = st.F80_svrBConnTimeout;
                                        Afn04F80svrAConnTimeout = st.F80_svrBConnTimeout;
                                        Afn04F80svrBConnTimeout = st.F80_svrAConnTimeout;
                                        Afn04F80maxReconnCount = st.F80_maxReconnCount;
                                        Afn04F80terminalId = st.F80_terminalId;
                                        Afn04F80Response = updateTIme;
                                        break;
                                    }
                            }
                            break;
                        }
                    case Comm.Afn0A:
                        {
                            ST_Afn0A st = (ST_Afn0A)pkt;
                            string updateTIme = $"更新时间：{st.TimeStr}";
                            switch (_curPkt.Fn)
                            {
                                case 1:
                                    {
                                        Afn04F1timeout = st.F1_TimeOut.ToString();
                                        Afn04F1repeatlimit = st.F1_RepeatTime.ToString();
                                        Afn04F1con = st.F1_Con.ToString();
                                        Afn04F1heartinterval = st.F1_HeartTime.ToString();
                                        Afn04F1Response = updateTIme;
                                        break;
                                    }
                                case 2:
                                    {
                                        Afn04F2AFN = st.F2_AFN;
                                        Afn04F2primaryIp = st.F2_PrimaryIp;
                                        Afn04F2primaryPort = st.F2_PrimaryPort;
                                        Afn04F2Pwd = st.F2_Pwd;
                                        Afn04F2realmname = st.F2_Realmname;
                                        Afn04F2realmname2 = st.F2_Realmname2;
                                        Afn04F2secondaryIp = st.F2_SecondaryIp;
                                        Afn04F2secondaryPort = st.F2_SecondaryPort;
                                        Afn04F2username = st.F2_Username;
                                        Afn04F2Response = updateTIme;
                                        break;
                                    }
                                case 3:
                                    {
                                        Afn04F3effect = st.F3_Effect.ToString();
                                        Afn04F3important = st.F3_Important.ToString();
                                        Afn04F3initiative = st.F3_Initiative.ToString();
                                        Afn04F3Response = updateTIme;
                                        break;
                                    }
                                case 73:
                                    {
                                        Afn04F73inlines = st.F73_Inlines;
                                        Afn04F73temppoints = st.F73_Temppoints;
                                        Afn04F73usercages = st.F73_Usercages;
                                        Afn04F73eleclock = ((int.Parse(st.F73_BackLock) << 2) + (int.Parse(st.F73_PublicLock) << 1) + int.Parse(st.F73_UserLock)).ToString();
                                        Afn04F73Response = updateTIme;
                                        break;
                                    }
                                case 75:
                                    {
                                        Afn04F75fanopentemp = st.F75_Fanopentemp.ToString();
                                        Afn04F75fanclosetemp = st.F75_Fanclosetemp.ToString();
                                        Afn04F75lasttime = st.F75_Lasttime.ToString();
                                        Afn04F75Response = updateTIme;
                                        break;
                                    }
                                case 76:
                                    {
                                        Afn04F76eleclockdevice = st.F76_Eleclockdevice;
                                        Afn04F76eleclockID = st.F76_EleclockID;
                                        Afn04F76atuoclose = st.F76_Atuoclose.ToString();
                                        Afn04F76alarmtime = st.F76_Alarmtime.ToString();
                                        Afn04F76Response = updateTIme;
                                        break;
                                    }
                                case 74:
                                    {
                                        Afn04F74templim = st.F74_Templim.ToString();
                                        Afn04F74temprecover = st.F74_Temprecover.ToString();
                                        Afn04F74wetlim = st.F74_Wetlim.ToString();
                                        Afn04F74wetrecover = st.F74_Wetrecover.ToString();
                                        Afn04F74AC220limon = st.F74_AC220limon.ToString();
                                        Afn04F74AC220recoveron = st.F74_AC220recoveron.ToString();
                                        Afn04F74AC220limoff = st.F74_AC220limoff.ToString();
                                        Afn04F74AC220recoveroff = st.F74_AC220recoveroff.ToString();
                                        Afn04F74AC24limon = st.F74_AC24limon.ToString();
                                        Afn04F74AC24recoveron = st.F74_AC24recoveron.ToString();
                                        Afn04F74AC24limoff = st.F74_AC24limoff.ToString();
                                        Afn04F74AC24recoveroff = st.F74_AC24recoveroff.ToString();
                                        Afn04F74DC24limon = st.F74_DC24limon.ToString();
                                        Afn04F74DC24recoveron = st.F74_DC24recoveron.ToString();
                                        Afn04F74DC24limoff = st.F74_DC24limoff.ToString();
                                        Afn04F74DC24recoveroff = st.F74_DC24recoveroff.ToString();
                                        Afn04F74usercagelim = st.F74_Usercagelim.ToString();
                                        Afn04F74usercagerecover = st.F74_Usercagerecover.ToString();
                                        Afn04F74fanerror = st.F74_Fanerror.ToString();
                                        Afn04F74overloadtime = st.F74_Overloadtime.ToString();
                                        Afn04F74Response = updateTIme;
                                        break;
                                    }
                            }
                            break;
                        }
                    case Comm.Afn0C:
                        {
                            ST_Afn0C st = (ST_Afn0C)pkt;
                            switch (_curPkt.Fn)
                            {
                                case 2: Afn0CF2Response = s; break;
                                case 73: Afn0CF73Response = s; break;
                                case 74: Afn0CF74Response = s; break;
                                case 75: Afn0CF75Response = s; break;
                                case 76: Afn0CF76Response = s; break;
                                case 77: Afn0CF77Response = s; break;
                                case 78: Afn0CF78Response = s; break;
                                case 81: Afn0CF81Response = s; break;
                                case 82: Afn0CF82Response = s; break;
                                case 83: Afn0CF83Response = s; break;
                                case 84: Afn0CF84Response = s; break;
                                case 85: Afn0CF85Response = s; break;
                                case 86: Afn0CF86Response = s; break;
                                case 87: Afn0CF87Response = s; break;
                                case 88: Afn0CF88Response = s; break;
                                case 89: Afn0CF89Response = s; break;
                            }
                            break;
                        }
                }
            }
            catch(Exception ex)
            {
                _dispatcher.Invoke(new Action(() =>
                {
                    _log.Error($"[OnRecvMsg]异常：{ex.Message}");
                }));
            }
        }
        #endregion

        #region Command

        #region Afn01
        private DelegateCommand afn01F1SendCmd;
        public DelegateCommand Afn01F1SendCmd => afn01F1SendCmd ?? (afn01F1SendCmd = new DelegateCommand(Afn01F1Send));
        void Afn01F1Send() 
        {
            _log.Info("=====发送Afn01 F1终端复位=====");
            byte[] payload = new byte[1] { (byte)Afn01F1ResetMode };
            doSend(Comm.Afn01, 0, 1, payload);
        }

        private DelegateCommand afn01F2SendCmd;
        public DelegateCommand Afn01F2SendCmd => afn01F2SendCmd ?? (afn01F2SendCmd = new DelegateCommand(Afn01F2Send));
        void Afn01F2Send()
        {
            _log.Info("=====发送Afn01 F2上行通信重连=====");
            doSend(Comm.Afn01, 0, 2);
        }

        private DelegateCommand afn01F3SendCmd;
        public DelegateCommand Afn01F3SendCmd => afn01F3SendCmd ?? (afn01F3SendCmd = new DelegateCommand(Afn01F3Send));
        void Afn01F3Send()
        {
            _log.Info("=====发送Afn01 F3终端模块复位=====");
            byte[] payload = new byte[1] { (byte)Afn01F3ResetModularMode };
            doSend(Comm.Afn01, 0, 3, payload);
        }
        #endregion

        #region Afn04/0A
        #region F1
        private DelegateCommand afn0AF1SendCmd;
        public DelegateCommand Afn0AF1SendCmd => afn0AF1SendCmd ?? (afn0AF1SendCmd = new DelegateCommand(Afn0AF1Send));
        void Afn0AF1Send()
        {
            _log.Info("=====查询Afn0A F1终端上行通信口通信参数=====");
            doSend(Comm.Afn0A, 0, 1);
        }
        private DelegateCommand afn04F1SendCmd;
        public DelegateCommand Afn04F1SendCmd => afn04F1SendCmd ?? (afn04F1SendCmd = new DelegateCommand(Afn04F1Send));
        void Afn04F1Send()
        {
            _log.Info("=====发送Afn04 F1终端上行通信口通信参数=====");
            byte[] payload = new byte[4];
            payload[0] = (byte)(int.Parse(Afn04F1timeout) % 256);
            payload[1] = (byte)((byte)(int.Parse(Afn04F1timeout) / 256) | (byte)(int.Parse(Afn04F1repeatlimit) << 4));
            payload[2] = (byte)int.Parse(Afn04F1con);
            payload[3] = (byte)int.Parse(Afn04F1heartinterval);
            doSend(Comm.Afn04, 0, 1, payload);
        }
        #endregion
        #region F2
        private DelegateCommand afn0AF2SendCmd;
        public DelegateCommand Afn0AF2SendCmd => afn0AF2SendCmd ?? (afn0AF2SendCmd = new DelegateCommand(Afn0AF2Send));
        void Afn0AF2Send()
        {
            _log.Info("=====查询Afn0A F2主站IP地址和端口=====");
            doSend(Comm.Afn0A, 0, 2);
        }
        private DelegateCommand afn04F2SendCmd;
        public DelegateCommand Afn04F2SendCmd => afn04F2SendCmd ?? (afn04F2SendCmd = new DelegateCommand(Afn04F2Send));
        void Afn04F2Send()
        {
            _log.Info("=====发送Afn04 F2主站IP地址和端口=====");
            byte[] payload = new byte[156];
            var ipArr = Afn04F2primaryIp.Split('.');
            int pos = 0;
            payload[pos++] = (byte)int.Parse(ipArr[0]);
            payload[pos++] = (byte)int.Parse(ipArr[1]);
            payload[pos++] = (byte)int.Parse(ipArr[2]);
            payload[pos++] = (byte)int.Parse(ipArr[3]);
            payload[pos++] = (byte)(int.Parse(Afn04F2primaryPort) % 256);
            payload[pos++] = (byte)(int.Parse(Afn04F2primaryPort) / 256);
            Encoding.ASCII.GetBytes(Afn04F2AFN).CopyTo(payload, pos); pos += 16;
            Encoding.ASCII.GetBytes(Afn04F2username).CopyTo(payload, pos); pos += 32;
            Encoding.ASCII.GetBytes(Afn04F2Pwd).CopyTo(payload, pos); pos += 32;
            Encoding.ASCII.GetBytes(Afn04F2realmname).CopyTo(payload, pos); pos += 32;
            ipArr = Afn04F2secondaryIp.Split('.');
            payload[pos++] = (byte)int.Parse(ipArr[0]);
            payload[pos++] = (byte)int.Parse(ipArr[1]);
            payload[pos++] = (byte)int.Parse(ipArr[2]);
            payload[pos++] = (byte)int.Parse(ipArr[3]);
            payload[pos++] = (byte)(int.Parse(Afn04F2secondaryPort) % 256);
            payload[pos++] = (byte)(int.Parse(Afn04F2secondaryPort) / 256);
            Encoding.ASCII.GetBytes(Afn04F2realmname2).CopyTo(payload, pos);
            doSend(Comm.Afn04, 0, 2, payload);
        }
        #endregion
        #region F3
        private DelegateCommand afn0AF3SendCmd;
        public DelegateCommand Afn0AF3SendCmd => afn0AF3SendCmd ?? (afn0AF3SendCmd = new DelegateCommand(Afn0AF3Send));
        void Afn0AF3Send()
        {
            _log.Info("=====查询Afn0A F3终端事件记录配置=====");
            doSend(Comm.Afn0A, 0, 3);
        }
        private DelegateCommand afn04F3SendCmd;
        public DelegateCommand Afn04F3SendCmd => afn04F3SendCmd ?? (afn04F3SendCmd = new DelegateCommand(Afn04F3Send));
        void Afn04F3Send()
        {
            _log.Info("=====发送Afn04 F3终端事件记录配置=====");
            byte[] payload = new byte[24];
            int pos = 0;
            FmtHelper.LongToBytes(long.Parse(Afn04F3effect), 8).ToArray().CopyTo(payload, pos); pos += 8;
            FmtHelper.LongToBytes(long.Parse(Afn04F3important), 8).ToArray().CopyTo(payload, pos); pos += 8;
            FmtHelper.LongToBytes(long.Parse(Afn04F3initiative), 8).ToArray().CopyTo(payload, pos);
            doSend(Comm.Afn04, 0, 3, payload);
        }
        #endregion
        #region F73
        private DelegateCommand afn0AF73SendCmd;
        public DelegateCommand Afn0AF73SendCmd => afn0AF73SendCmd ?? (afn0AF73SendCmd = new DelegateCommand(Afn0AF73Send));
        void Afn0AF73Send()
        {
            _log.Info("=====查询Afn0A F73设备基本信息=====");
            doSend(Comm.Afn0A, 0, 73);
        }
        private DelegateCommand afn04F73SendCmd;
        public DelegateCommand Afn04F73SendCmd => afn04F73SendCmd ?? (afn04F73SendCmd = new DelegateCommand(Afn04F73Send));
        void Afn04F73Send()
        {
            _log.Info("=====发送Afn04 F3设备基本信息=====");
            byte[] payload = new byte[4];
            int pos = 0;
            payload[pos++] = (byte)int.Parse(Afn04F73inlines);
            payload[pos++] = (byte)int.Parse(Afn04F73temppoints);
            payload[pos++] = (byte)int.Parse(Afn04F73usercages);
            payload[pos++] = (byte)int.Parse(Afn04F73eleclock);
            doSend(Comm.Afn04, 0, 73, payload);
        }
        #endregion
        #region F75
        private DelegateCommand afn0AF75SendCmd;
        public DelegateCommand Afn0AF75SendCmd => afn0AF75SendCmd ?? (afn0AF75SendCmd = new DelegateCommand(Afn0AF75Send));
        void Afn0AF75Send()
        {
            _log.Info("=====查询Afn0A F75风扇温度=====");
            doSend(Comm.Afn0A, 0, 75);
        }
        private DelegateCommand afn04F75SendCmd;
        public DelegateCommand Afn04F75SendCmd => afn04F75SendCmd ?? (afn04F75SendCmd = new DelegateCommand(Afn04F75Send));
        void Afn04F75Send()
        {
            _log.Info("=====发送Afn04 F75风扇温度=====");
            byte[] payload = new byte[7];
            int pos = 0;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F75fanopentemp), 3).ToArray().CopyTo(payload, pos); pos += 3;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F75fanclosetemp), 3).ToArray().CopyTo(payload, pos); pos += 3;
            payload[pos] = (byte)int.Parse(Afn04F75lasttime);
            doSend(Comm.Afn04, 0, 75, payload);
        }
        #endregion
        #region F76
        private DelegateCommand afn0AF76SendCmd;
        public DelegateCommand Afn0AF76SendCmd => afn0AF76SendCmd ?? (afn0AF76SendCmd = new DelegateCommand(Afn0AF76Send));
        void Afn0AF76Send()
        {
            _log.Info("=====查询Afn0A F76电子锁=====");
            byte[] payload = new byte[6];
            FmtHelper.LongToBCD(long.Parse(Afn04F76eleclockID), 4).CopyTo(payload, 0);
            doSend(Comm.Afn0A, (int)Afn04F76Door, 76, payload);
        }
        private DelegateCommand afn04F76SendCmd;
        public DelegateCommand Afn04F76SendCmd => afn04F76SendCmd ?? (afn04F76SendCmd = new DelegateCommand(Afn04F76Send));
        void Afn04F76Send()
        {
            _log.Info("=====发送Afn04 F76电子锁=====");
            byte[] payload = new byte[12];
            int pos = 0;
            FmtHelper.LongToBCD(long.Parse(Afn04F76eleclockdevice), 4).CopyTo(payload, pos); pos += 4;
            FmtHelper.LongToBCD(long.Parse(Afn04F76eleclockID), 4).CopyTo(payload, pos); pos += 4;
            FmtHelper.LongToBCD(long.Parse(Afn04F76atuoclose), 2).CopyTo(payload, pos); pos += 2;
            FmtHelper.LongToBCD(long.Parse(Afn04F76alarmtime), 2).CopyTo(payload, pos);
            doSend(Comm.Afn04, (int)Afn04F76Door, 76, payload);
        }
        #endregion
        #region F74
        private DelegateCommand afn0AF74SendCmd;
        public DelegateCommand Afn0AF74SendCmd => afn0AF74SendCmd ?? (afn0AF74SendCmd = new DelegateCommand(Afn0AF74Send));
        void Afn0AF74Send()
        {
            _log.Info("=====查询Afn0A F74事件检测阈值=====");
            doSend(Comm.Afn0A, 0, 74);
        }
        private DelegateCommand afn04F74SendCmd;
        public DelegateCommand Afn04F74SendCmd => afn04F74SendCmd ?? (afn04F74SendCmd = new DelegateCommand(Afn04F74Send));
        void Afn04F74Send()
        {
            _log.Info("=====发送Afn04 F74事件检测阈值=====");
            byte[] payload = new byte[44];
            int pos = 0;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74templim), 3).ToArray().CopyTo(payload, pos); pos += 3;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74temprecover), 3).ToArray().CopyTo(payload, pos); pos += 3;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74wetlim), 3).ToArray().CopyTo(payload, pos); pos += 3;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74wetrecover), 3).ToArray().CopyTo(payload, pos); pos += 3;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC220limon), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC220recoveron), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC220limoff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC220recoveroff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC24limon), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC24recoveron), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC24limoff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74AC24recoveroff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74DC24limon), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74DC24recoveron), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74DC24limoff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74DC24recoveroff), 2).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.uGetAppendB2(double.Parse(Afn04F74usercagelim)).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.uGetAppendB2(double.Parse(Afn04F74usercagerecover)).ToArray().CopyTo(payload, pos); pos += 2;
            FmtHelper.DoubleToBytes(double.Parse(Afn04F74fanerror), 3).ToArray().CopyTo(payload, pos); pos += 3;
            payload[pos] = (byte)int.Parse(Afn04F74overloadtime);
            doSend(Comm.Afn04, 0, 74, payload);
        }
        #endregion
        #region F80
        private DelegateCommand afn04F80SendCmd;
        public DelegateCommand Afn04F80SendCmd => afn04F80SendCmd ?? (afn04F80SendCmd = new DelegateCommand(Afn04F80Send));
        void Afn04F80Send()
        {
            _log.Info("=====发送Afn04 F80自定义参数=====");
            byte[] payload = new byte[78];
            int pos = 0;
            //厂商代号
            if (Afn04F80manuId.Length < 4)
                Afn04F80manuId = "0000";
            for (int i = 0; i < 4; ++i)
            {
                payload[pos++] = (byte)Afn04F80manuId[i];
            }
            //厂商设备编号
            if (Afn04F80deviceId.Length < 8)
                Afn04F80deviceId = "00000000";
            for (int i = 0; i < 8; ++i)
            {
                payload[pos++] = (byte)Afn04F80deviceId[i];
            }
            //终端软件版本号
            if (Afn04F80version.Length < 8)
                Afn04F80version = "00000000";
            for (int i = 0; i < 8; ++i)
            {
                payload[pos++] = (byte)Afn04F80version[i];
            }
            //软件发布日期
            var bt = StrHelper.StrToHexByte(Afn04F80publishDate.Replace('-', ' '));
            for (int i = 0; i < bt.Length; ++i)
            {
                payload[pos++] = bt[i];
            }
            //终端通信协议版本号
            if (Afn04F80protocolVer.Length < 8)
                Afn04F80protocolVer = "00000000";
            for (int i = 0; i < 8; ++i)
            {
                payload[pos++] = (byte)Afn04F80protocolVer[i];
            }
            //硬件版本号
            if (Afn04F80hardwareVer.Length < 8)
                Afn04F80hardwareVer = "00000000";
            for (int i = 0; i < 8; ++i)
            {
                payload[pos++] = (byte)Afn04F80hardwareVer[i];
            }
            //硬件发布日期
            bt = StrHelper.StrToHexByte(Afn04F80hardwarePublish.Replace('-', ' '));
            for (int i = 0; i < bt.Length; ++i)
            {
                payload[pos++] = bt[i];
            }
            //终端出厂日期
            bt = StrHelper.StrToHexByte(Afn04F80leaveFactory.Replace('-', ' '));
            for (int i = 0; i < bt.Length; ++i)
            {
                payload[pos++] = bt[i];
            }
            //IP地址
            string[] strArr = Afn04F80ip.Split('.');
            for (int i = 0; i < strArr.Length; ++i)
            {
                payload[pos++] = (byte)int.Parse(strArr[i]);
            }
            //网关地址
            strArr = Afn04F80gateway.Split('.');
            for (int i = 0; i < strArr.Length; ++i)
            {
                payload[pos++] = (byte)int.Parse(strArr[i]);
            }
            //子网掩码地址
            strArr = Afn04F80submask.Split('.');
            for (int i = 0; i < strArr.Length; ++i)
            {
                payload[pos++] = (byte)int.Parse(strArr[i]);
            }
            //MAC地址
            bt = StrHelper.StrToHexByte(Afn04F80mac);
            for (int i = 0; i < bt.Length; ++i)
            {
                payload[pos++] = bt[i];
            }
            //串口波特率
            payload[pos++] = PktUtils.getBaudRateByte(Afn04F80SelbaudRate1);
            payload[pos++] = PktUtils.getBaudRateByte(Afn04F80SelbaudRate2);
            payload[pos++] = PktUtils.getBaudRateByte(Afn04F80SelbaudRate3);
            payload[pos++] = (byte)Afn04F80svrBReconnTime;
            payload[pos++] = (byte)Afn04F80svrAReconnTime;
            payload[pos++] = (byte)Afn04F80svrBConnTimeout;
            payload[pos++] = (byte)Afn04F80svrAConnTimeout;
            payload[pos++] = (byte)Afn04F80maxReconnCount;
            //终端ID
            if (Afn04F80terminalId.Length < 14)
                Afn04F80terminalId = "00000000000000";
            bt = StrHelper.StrToHexByte(Afn04F80terminalId);
            for (int i = 6; i >= 0; --i)
            {
                payload[pos++] = bt[i];
            }
            doSend(Comm.Afn04, 0, 80, payload);
        }
        #endregion
        #endregion

        #region Afn05
        #region F31
        private DelegateCommand afn05F31SendCmd;
        public DelegateCommand Afn05F31SendCmd => afn05F31SendCmd ?? (afn05F31SendCmd = new DelegateCommand(Afn05F31Send));
        void Afn05F31Send()
        {
            _log.Info("=====发送Afn05 F31设备校时=====");
            var payload = new byte[6];
            payload[0] = (byte)FmtHelper.DecToBCD(DateTime.Now.Second);
            payload[1] = (byte)FmtHelper.DecToBCD(DateTime.Now.Minute);
            payload[2] = (byte)FmtHelper.DecToBCD(DateTime.Now.Hour);
            payload[3] = (byte)FmtHelper.DecToBCD(DateTime.Now.Day);
            payload[4] = (byte)(FmtHelper.DecToBCD(DateTime.Now.Month) + unchecked((int)DateTime.Now.DayOfWeek) * 32);
            payload[5] = (byte)FmtHelper.DecToBCD(unchecked(DateTime.Now.Year % 100));
            doSend(Comm.Afn05, 0, 31, payload);
        }
        #endregion
        #region F73

        private DelegateCommand<object> afn05F73SendCmd;
        public DelegateCommand<object> Afn05F73SendCmd => afn05F73SendCmd ?? (afn05F73SendCmd = new DelegateCommand<object>(Afn05F73Send));
        void Afn05F73Send(object door)
        {
            string pn = (string)door;
            _log.Info($"=====发送Afn05 F73箱门{door}开启=====");
            doSend(Comm.Afn05, int.Parse(pn), 73);
        }
        #endregion
        #region F74
        private DelegateCommand afn05F74SendCmd;
        public DelegateCommand Afn05F74SendCmd => afn05F74SendCmd ?? (afn05F74SendCmd = new DelegateCommand(Afn05F74Send));
        void Afn05F74Send()
        {
            _log.Info($"=====发送Afn05 F74进线遥控操作=====");
            var payload = new byte[1] { (byte)Afn05F74InlineMode };
            doSend(Comm.Afn05, (int)Afn05F74InlineNo, 74, payload);
        }
        #endregion
        #region F75
        private DelegateCommand afn05F75SendCmd;
        public DelegateCommand Afn05F75SendCmd => afn05F75SendCmd ?? (afn05F75SendCmd = new DelegateCommand(Afn05F75Send));
        void Afn05F75Send()
        {
            _log.Info($"=====发送Afn05 F75用户仓遥控操作=====");
            var payload = new byte[2] { (byte)Afn05F75OptType, (byte)Afn05F75Branch };
            doSend(Comm.Afn05, (int)Afn05F75CabinNo, 75, payload);
        }
        #endregion
        #endregion

        #region Afn09
        #region F1
        private DelegateCommand afn09F1QueryCmd;
        public DelegateCommand Afn09F1QueryCmd => afn09F1QueryCmd ?? (afn09F1QueryCmd = new DelegateCommand(Afn09F1Query));
        void Afn09F1Query()
        {
            _log.Info("=====查询Afn09 F1终端信息=====");
            doSend(Comm.Afn09, 0, 1);
        }
        #endregion
        #region F9
        private DelegateCommand afn09F9QueryCmd;
        public DelegateCommand Afn09F9QueryCmd => afn09F9QueryCmd ?? (afn09F9QueryCmd = new DelegateCommand(Afn09F9Query));
        void Afn09F9Query()
        {
            _log.Info("=====查询Afn09 F9电子锁信息=====");
            doSend(Comm.Afn09, 0, 9);
        }
        #endregion
        #region F80
        private DelegateCommand afn09F80SendCmd;
        public DelegateCommand Afn09F80SendCmd => afn09F80SendCmd ?? (afn09F80SendCmd = new DelegateCommand(Afn09F80Send));
        void Afn09F80Send()
        {
            _log.Info("=====查询Afn09 F80自定义参数=====");
            doSend(Comm.Afn09, 0, 80);
        }
        #endregion
        #endregion

        #region Afn0C
        #region F2
        private DelegateCommand afn0CF2QueryCmd;
        public DelegateCommand Afn0CF2QueryCmd => afn0CF2QueryCmd ?? (afn0CF2QueryCmd = new DelegateCommand(Afn0CF2Query));
        void Afn0CF2Query()
        {
            _log.Info("=====查询Afn0C F2终端时钟=====");
            doSend(Comm.Afn0C, 0, 2);
        }
        #endregion
        #region F73
        private DelegateCommand afn0CF73QueryCmd;
        public DelegateCommand Afn0CF73QueryCmd => afn0CF73QueryCmd ?? (afn0CF73QueryCmd = new DelegateCommand(Afn0CF73Query));
        void Afn0CF73Query()
        {
            _log.Info("=====查询Afn0C F73设备箱温度=====");
            doSend(Comm.Afn0C, (int)Afn0CF73Box, 73);
        }
        #endregion
        #region F74
        private DelegateCommand afn0CF74QueryCmd;
        public DelegateCommand Afn0CF74QueryCmd => afn0CF74QueryCmd ?? (afn0CF74QueryCmd = new DelegateCommand(Afn0CF74Query));
        void Afn0CF74Query()
        {
            _log.Info("=====查询Afn0C F74设备箱湿度=====");
            doSend(Comm.Afn0C, 0, 74);
        }
        #endregion
        #region F75
        private DelegateCommand afn0CF75QueryCmd;
        public DelegateCommand Afn0CF75QueryCmd => afn0CF75QueryCmd ?? (afn0CF75QueryCmd = new DelegateCommand(Afn0CF75Query));
        void Afn0CF75Query()
        {
            _log.Info("=====查询Afn0C F75状态量=====");
            doSend(Comm.Afn0C, 0, 75);
        }
        #endregion
        #region F76
        private DelegateCommand afn0CF76QueryCmd;
        public DelegateCommand Afn0CF76QueryCmd => afn0CF76QueryCmd ?? (afn0CF76QueryCmd = new DelegateCommand(Afn0CF76Query));
        void Afn0CF76Query()
        {
            _log.Info("=====查询Afn0C F76终端当前控制状态=====");
            doSend(Comm.Afn0C, 0, 76);
        }
        #endregion
        #region F77
        private DelegateCommand afn0CF77QueryCmd;
        public DelegateCommand Afn0CF77QueryCmd => afn0CF77QueryCmd ?? (afn0CF77QueryCmd = new DelegateCommand(Afn0CF77Query));
        void Afn0CF77Query()
        {
            _log.Info("=====查询Afn0C F77终端事件计数器当前值=====");
            doSend(Comm.Afn0C, 0, 77);
        }
        #endregion
        #region F78
        private DelegateCommand afn0CF78QueryCmd;
        public DelegateCommand Afn0CF78QueryCmd => afn0CF78QueryCmd ?? (afn0CF78QueryCmd = new DelegateCommand(Afn0CF78Query));
        void Afn0CF78Query()
        {
            _log.Info("=====查询Afn0C F78电子锁锁舌状态=====");
            doSend(Comm.Afn0C, (int)Afn0CF78Door, 78);
        }
        #endregion
        #region F81
        private DelegateCommand afn0CF81QueryCmd;
        public DelegateCommand Afn0CF81QueryCmd => afn0CF81QueryCmd ?? (afn0CF81QueryCmd = new DelegateCommand(Afn0CF81Query));
        void Afn0CF81Query()
        {
            _log.Info("=====查询Afn0C F81用户舱电压、电流=====");
            doSend(Comm.Afn0C, (int)Afn0CF81CabinNo, 81);
        }
        #endregion
        #region F82
        private DelegateCommand afn0CF82QueryCmd;
        public DelegateCommand Afn0CF82QueryCmd => afn0CF82QueryCmd ?? (afn0CF82QueryCmd = new DelegateCommand(Afn0CF82Query));
        void Afn0CF82Query()
        {
            _log.Info("=====查询Afn0C F82用户舱功率=====");
            doSend(Comm.Afn0C, (int)Afn0CF81CabinNo, 82);
        }
        #endregion
        #region F83
        private DelegateCommand afn0CF83QueryCmd;
        public DelegateCommand Afn0CF83QueryCmd => afn0CF83QueryCmd ?? (afn0CF83QueryCmd = new DelegateCommand(Afn0CF83Query));
        void Afn0CF83Query()
        {
            _log.Info("=====查询Afn0C F83用户舱当日电能量=====");
            doSend(Comm.Afn0C, (int)Afn0CF81CabinNo, 83);
        }
        #endregion
        #region F84
        private DelegateCommand afn0CF84QueryCmd;
        public DelegateCommand Afn0CF84QueryCmd => afn0CF84QueryCmd ?? (afn0CF84QueryCmd = new DelegateCommand(Afn0CF84Query));
        void Afn0CF84Query()
        {
            _log.Info("=====查询Afn0C F83用户舱当月电能量=====");
            doSend(Comm.Afn0C, (int)Afn0CF81CabinNo, 84);
        }
        #endregion
        #region F85
        private DelegateCommand afn0CF85QueryCmd;
        public DelegateCommand Afn0CF85QueryCmd => afn0CF85QueryCmd ?? (afn0CF85QueryCmd = new DelegateCommand(Afn0CF85Query));
        void Afn0CF85Query()
        {
            _log.Info("=====查询Afn0C F85进线电压、电流=====");
            doSend(Comm.Afn0C, (int)Afn0CF85InlineNo, 85);
        }
        #endregion
        #region F86
        private DelegateCommand afn0CF86QueryCmd;
        public DelegateCommand Afn0CF86QueryCmd => afn0CF86QueryCmd ?? (afn0CF86QueryCmd = new DelegateCommand(Afn0CF86Query));
        void Afn0CF86Query()
        {
            _log.Info("=====查询Afn0C F86进线功率=====");
            doSend(Comm.Afn0C, (int)Afn0CF85InlineNo, 86);
        }
        #endregion
        #region F87
        private DelegateCommand afn0CF87QueryCmd;
        public DelegateCommand Afn0CF87QueryCmd => afn0CF87QueryCmd ?? (afn0CF87QueryCmd = new DelegateCommand(Afn0CF87Query));
        void Afn0CF87Query()
        {
            _log.Info("=====查询Afn0C F87当日电能量=====");
            doSend(Comm.Afn0C, (int)Afn0CF85InlineNo, 87);
        }
        #endregion
        #region F88
        private DelegateCommand afn0CF88QueryCmd;
        public DelegateCommand Afn0CF88QueryCmd => afn0CF88QueryCmd ?? (afn0CF88QueryCmd = new DelegateCommand(Afn0CF88Query));
        void Afn0CF88Query()
        {
            _log.Info("=====查询Afn0C F88当月电能量=====");
            doSend(Comm.Afn0C, (int)Afn0CF85InlineNo, 88);
        }
        #endregion
        #region F89
        private DelegateCommand afn0CF89QueryCmd;
        public DelegateCommand Afn0CF89QueryCmd => afn0CF89QueryCmd ?? (afn0CF89QueryCmd = new DelegateCommand(Afn0CF89Query));
        void Afn0CF89Query()
        {
            _log.Info("=====查询Afn0C F89风扇功率=====");
            doSend(Comm.Afn0C, 0, 89);
        }
        #endregion
        #endregion

        #endregion

        #region 发送下行报文
        void doSend(byte afn, int pn, int fn, byte[] payload = null)
        {
            if(string.IsNullOrEmpty(_selClientKey) 
                || !_keyTermIdDict.ContainsKey(_selClientKey))
            {
                _log.Warn("未选择客户端连接，或无可用客户端！");
                return;
            }
            var pkt = new BasePkt();
            string tid = _cfgSvr.Get().net.is4GMode ? _keyTermIdDict[_selClientKey] : _TerminalId;
            pkt.Init(afn, pn, fn, tid);
            byte[] data = PktUtils.MakeSendPkg(pkt, payload);
            SendData(data);
            _curPkt = pkt;
        }
        private void SendData(byte[] data)
        {
            TcpServer tcpSrv = MainWindowViewModel.TCPServer;
            tcpSrv.SendData(_selClientKey, data);
            _log.Info($"[{_selClientKey}]发送报文→{BitConverter.ToString(data).ToLower()}");
        }
        #endregion
    }
}
